﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using NPMonitor.DataModel;

namespace NPMonitor.Implementation.Mail
{
    public class SendMail
    {
        /// <summary>
        /// 从数据库中读取未发送的邮件，并发送
        /// </summary>
        public void Execute()
        {
            List<MailData> list = StoreManager.Current.MailStore.GetNoSendMailList(TakeMailCount);
            foreach (var mailData in list)
            {
                try
                {
                    StoreManager.Current.MailStore.UpdateMail(mailData.MailId, MailStatus.Sending);
                    Send(mailData);
                    StoreManager.Current.MailStore.UpdateMail(mailData.MailId, MailStatus.Successed);
                }
                catch
                {
                    StoreManager.Current.MailStore.UpdateMail(mailData.MailId, MailStatus.Failed);
                }
            }
        }

        private void Send(MailData mailData)
        {
            MailHelper mailHelper = new MailHelper(Smtp, Port);
            mailHelper.From = From;
            mailHelper.Subject = mailData.Subject;
            string[] strTemp = mailData.Body.Split('|');
            mailHelper.Body = GetHtmlBody(mailData.CreateBy, strTemp[0], strTemp[1], mailData.TemplateName);

            List<string> toList = new List<string>();
            List<string> ccList = new List<string>();

            if (!string.IsNullOrEmpty(mailData.To))
            {
                toList = mailData.To.Split(';').ToList();
            }

            if (!string.IsNullOrEmpty(mailData.Cc))
            {
                ccList = mailData.Cc.Split(';').ToList();
            }
            mailHelper.Send(toList, ccList);
        }

        private int TakeMailCount
        {
            get
            {
                string count = ConfigurationManager.AppSettings["TakeMailCount"];
                return Convert.ToInt32(count);
            }
        }

        private string Smtp
        {
            get { return ConfigurationManager.AppSettings["SMTP"]; }
        }
        private int Port
        {
            get
            {
                string port = ConfigurationManager.AppSettings["Port"];
                return Convert.ToInt32(port);
            }
        }
        private string From
        {
            get { return ConfigurationManager.AppSettings["From"]; }
        }

        private string GetHtmlBody(string userName, string jobId, string jobName, string templateName)
        {
            NotificationEvent notifactionEvent = (NotificationEvent)Enum.Parse(typeof(NotificationEvent), templateName, true);
            string result = "";
            switch (notifactionEvent)
            {
                case NotificationEvent.JobSuccess:
                    result = CommUtil.CreateHtmlBody("JobNotification.htm", userName, jobId, jobName, "Success");
                    break;
                case NotificationEvent.JobFailed:
                    result = CommUtil.CreateHtmlBody("JobNotification.htm", userName, jobId, jobName, "Failed");
                    break;
                case NotificationEvent.Report:
                    result = CommUtil.CreateHtmlBody("JobNotification.htm", userName, jobId, jobName, "Success");
                    break;
            }

            return result;
        }
    }
}
